﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace badminton.Pages
{
    public partial class MyTraining : System.Web.UI.Page
    {
        protected global::System.Web.UI.WebControls.TextBox TextBox1;
        protected Label Label1;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Session["Login"]==null)
            {
                Response.Write("<script type='text/javascript'>alert('您还未登录，请先登录！');window.open('../index.aspx','_self');</script>");
                return;
            }
            LoginSession login = Session["Login"] as LoginSession;

            if (login.m_loginType!= "学员")
            {
                Response.Write("<script type='text/javascript'>alert('你不是学员用户！');history.go(-1);</script>");
                return;
            }

            int studentId = login.m_loginId;

            BadmintonEntities bde = new BadmintonEntities();
            Student student = bde.Student.FirstOrDefault(p => p.id == studentId);
            if (student == null)
            {
                Response.Write("<script type='text/javascript'>alert('未能查找到您的登录用户，请重新登录！');window.open('../index.aspx','_self');</script>");
                return;
            }

            // bind the traing list
            if (!IsPostBack)
            {
                // bind the traing couses list
                bindTrainCourse();

            }

            MyAccordion.Panes.Clear();
            AjaxControlToolkit.AccordionPane panel = new AjaxControlToolkit.AccordionPane();
            panel.ID = "AccordPaneHall" + student.id.ToString();
            panel.HeaderContainer.Controls.Add(new LiteralControl(student.name + "的培训"));
            foreach (Training tra in student.Training)
            {
                HyperLink hlChildMenu = new HyperLink();
                hlChildMenu.Text = tra.name + "<br/>";
                hlChildMenu.NavigateUrl = "./MyTraining.aspx?trainingid=" + tra.id.ToString();
                panel.ContentContainer.Controls.Add(hlChildMenu);
            }
            MyAccordion.Panes.Add(panel);


                  
        }

        public string GetMyName()
        {
            LoginSession login = Session["Login"] as LoginSession;
            if (login==null||login.m_loginType != "学员")
            {
                return "我的培训";
            }

            int studentId = login.m_loginId;
            BadmintonEntities bde = new BadmintonEntities();
            Student student = bde.Student.FirstOrDefault(p => p.id == studentId);
            if (student==null)
            {
                return "我的培训";
            }
            return student.name + "的培训";
        }

        protected void bindTrainCourse()
        {
            BadmintonEntities bde = new BadmintonEntities();
            LoginSession login = Session["Login"] as LoginSession;
            int studentId = login.m_loginId;

            Student student = bde.Student.FirstOrDefault(p => p.id == studentId);
            if (student == null)
            {
                Response.Write("<script type='text/javascript'>alert('未能查找到您的登录用户，请重新登录！');window.open('../index.aspx','_self');</script>");
                return;
            }

            int traingId = -1;
            if(this.Request.QueryString["trainingid"]!=null)
            {
                traingId = Convert.ToInt32(this.Request.QueryString["trainingid"]);
            }

            if (traingId==-1 && student.Training.Count>0)
            {
                traingId = student.Training.FirstOrDefault().id;
            }

            if (traingId >=0)
            {
                ListView2.DataSource = bde.TrainingCourse.Where(p => p.trainingId == traingId).OrderBy(p => p.startTime);
                ListView2.DataBind();
            }
        }

        public System.Drawing.Color GetBackColor(object id)
        {

            return System.Drawing.Color.FromArgb(0, 255, 255);
        }

        public string GetURL(object id)
        {
            return "./MyTraining.aspx?trainingid=" + id.ToString();
        }

        public string GetCourseName(object id)
        {
            int tcid = (int)id;
            BadmintonEntities bde = new BadmintonEntities();

            TrainingCourse tc = bde.TrainingCourse.FirstOrDefault(p => p.id == tcid);
    
            if (tc!=null)
            {
                return "课程： "+ tc.name;
            }

            return "";
        }

        public string GetGrade(object id)
        {
            int tcid = (int)id;
            LoginSession login = Session["Login"] as LoginSession;
            int studentId = login.m_loginId;

            BadmintonEntities bde = new BadmintonEntities();
            TrainingCourseGrading tcg = bde.TrainingCourseGrading.FirstOrDefault(p => p.courseId == tcid && p.studentId == studentId);
            if (tcg != null)
            {
                string str="成绩：";
                if (tcg.grade==null)
                {
                    return str;
                }
                for (int ii = 0; ii < tcg.grade; ii++ )
                {
                    str = str + "<img src='../images/star.jpg' />";
                }

                return str;
            }

            return "";

        }

        public string GetTrainerReview(object id)
        {
            int tcid = (int)id;
            LoginSession login = Session["Login"] as LoginSession;
            int studentId = login.m_loginId;

            BadmintonEntities bde = new BadmintonEntities();
            TrainingCourseGrading tcg = bde.TrainingCourseGrading.FirstOrDefault(p => p.courseId == tcid && p.studentId == studentId);
            if (tcg!=null)
            {
                return tcg.comment;
            }
            return "";
        }

        public string GetFeedBack(object id)
        {
            int tcid = (int)id;
            LoginSession login = Session["Login"] as LoginSession;
            int studentId = login.m_loginId;

            BadmintonEntities bde = new BadmintonEntities();
            TrainingCourseGrading tcg = bde.TrainingCourseGrading.FirstOrDefault(p => p.courseId == tcid && p.studentId == studentId);
            if (tcg != null)
            {
                return tcg.studentFeedback;
            }
            return "";
        }

        public void OnFeedBack(object sender, CommandEventArgs e)
        {
            int itemIdx = Convert.ToInt32(e.CommandName);
            TextBox txt = ListView2.Items[itemIdx].FindControl("TextBox1") as TextBox;

            if (txt == null)
            {
                return;
            }

            if (txt.Text.Length==0)
            {
                return;
            }

            int tcid = Convert.ToInt32(e.CommandArgument);
            LoginSession login = Session["Login"] as LoginSession;
            int studentId = login.m_loginId;

            BadmintonEntities bde = new BadmintonEntities();
            TrainingCourseGrading tcg = bde.TrainingCourseGrading.FirstOrDefault(p => p.courseId == tcid && p.studentId == studentId);
            if (tcg==null)
            {
                tcg = new TrainingCourseGrading();
                tcg.courseId = tcid;
                tcg.studentId = studentId;
                bde.TrainingCourseGrading.AddObject(tcg);
            }

            if (tcg.studentFeedback != txt.Text)
            {
                tcg.studentFeedback = txt.Text;
            }
            bde.SaveChanges();

            bindTrainCourse();
        }
    }
}